<?php
declare (strict_types = 1);
namespace app\user\model\teacher\admin;

use app\common\model\BaseModel;
use app\user\event\subscribes\user\admin\TeacherLevel as TeacherLevelSubscribe;
use think\facade\Event;
use think\model\concern\SoftDelete;
use app\common\model\Attach;

/**
 * 定义等级数据模型
 */
class TeacherLevel extends BaseModel
{
    use SoftDelete;
    /**
     * 表名
     * @var string
     */
    protected $name = 'teacher_level';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;
    /**
     * 不写入创建时间
     * @var boolean
     */
    protected $createTime = false;

    /**
     * 时间显示格式
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     * @var string
     */
    protected $pk = 'id';

    // 定义全局的查询范围
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

    
    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     * @return   string
     */
    public static function onBeforeInsert($teacher)
    {
        // 机构
        $teacher->mhm_id = $teacher->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 成功创建之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-18
     */
    public static function onAfterInsert($teacher)
    {
        
        Event::subscribe(TeacherLevelSubscribe::class);
        Event::trigger('TeacherLevelCreated', $teacher);
    }

    /**
     * 成功修改之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-18
     */
    public static function onAfterUpdate($teacher)
    {
        Event::subscribe(TeacherLevelSubscribe::class);
        Event::trigger('TeacherLevelUpdated', $teacher);
    }

    /**
     * 成功删除之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-18
     */
    public static function onAfterDelete($teacher)
    {
        Event::subscribe(TeacherLevelSubscribe::class);
        Event::trigger('TeacherLevelDeleted', $teacher);
    }

    /**
     * 获取图标事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2022-09-21
     */
    public function iconUrl()
    {
        return $this->belongsTo(Attach::class, 'icon')->bind(['icon_url'=>'fileurl']);
    }
}
